package com.qkbb.admin.kuibu.qkbb.funcation;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qkbb.admin.kuibu.qkbb.JavaBean.Point;
import com.qkbb.admin.kuibu.qkbb.url.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/1/28.
 */
public class GetPoint {
    private List<Point> list;
    public List<Point> getPoint(Context context){
        String user_token=SDCardHelper.loadFileFromSdCard("user_token",context);
        list=new ArrayList<Point>();
        RequestQueue queue= Volley.newRequestQueue(context);
        StringRequest stringRequest=new StringRequest(Url.GETPOINT1 + user_token + Url.GETPOINT2 + "1", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("S", s);
                try {
                    JSONObject jsonObject =new JSONObject(s);
                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                    JSONArray jsonArray=jsonObject1.getJSONArray("Coords");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject2=jsonArray.getJSONObject(i);
                        Point point=new Point();
                        point.setDescription(jsonObject2.getString("description"));
                        point.setIdxno(jsonObject2.getInt("idxno"));
                        point.setLatitude(jsonObject2.getDouble("latitude"));
                        point.setLongitude(jsonObject2.getDouble("longitude"));
                        point.setRoadid(jsonObject2.getInt("roadid"));
                        point.setRoadlength(jsonObject2.getInt("roadlength"));
                        point.setRoadsteplength(jsonObject2.getInt("roadsteplength"));
                        point.setSteppercent(jsonObject2.getInt("roadsteplength"));
                        list.add(point);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("volleyError", "" + volleyError);

            }
        });
        queue.add(stringRequest);
        return list;
    }

}
